import { api } from '@/utils/request'
import type { 
  User, 
  PageParams,
  PageResult,
  ApiResponse ,
  UserByPhoneResponse
} from '@/types'

// 用户分页查询参数
export interface UserPageParams extends PageParams {
  id?: number
  userType?: string
  nickName?: string
  phone?: string
  email?: string
  userStatus?: string
  schoolId?: number
  grade?: string
  createTime?: string
  isDeleted?: number
}

export class UserService {
  /**
   * 分页查询用户列表
   */
  static async getUserPage(params: UserPageParams): Promise<PageResult<User>> {
    const response = await api.post<PageResult<User>>('/auth/api-admin/authUser/page', params)
    return response.data
  }

  /**
   * 获取用户详情
   */
  static async getUserDetail(id: number): Promise<User> {
    const response = await api.get<User>(`/auth/api-admin/authUser/get/${id}`)
    return response.data
  }

  /**
   * 新增用户
   */
  static async createUser(userData: Partial<User>): Promise<boolean> {
    const response = await api.post<boolean>('/auth/api-admin/authUser/create', userData)
    return response.data
  }

  /**
   * 更新用户
   */
  static async updateUser(userData: Partial<User>): Promise<boolean> {
    const response = await api.post<boolean>('/auth/api-admin/authUser/update', userData)
    return response.data
  }

  /**
   * 删除用户
   */
  static async deleteUser(id: number): Promise<boolean> {
    const response = await api.delete<boolean>(`/auth/api-admin/authUser/delete/${id}`)
    return response.data
  }

  /**
   * 获取当前用户信息
   */
  static async getCurrentUserInfo(): Promise<User> {
    const response = await api.get<User>('/auth/api-admin/authUser/getUserInfo')
    return response.data
  }

   /**
   * 根据手机号查询用户信息
   */
   static async getUserByPhone(phone: string): Promise<UserByPhoneResponse> {
    const response = await api.get<UserByPhoneResponse>(`/auth/api-admin/authUser/getByPhone/${phone}`)
    return response.data
  }

  /**
   * 切换用户状态（封禁/解封）
   */
  static async toggleUserStatus(id: number): Promise<boolean> {
    const response = await api.post<boolean>(`/auth/api-admin/authUser/toggleStatus/${id}`)
    return response.data
  }
}

// 默认导出单例实例
export const userService = UserService 